<?php

function clean_file_name($Raw){
    $Raw = trim(strtolower($Raw));
    $RemoveChars  = array( "([\40])" , "([^a-zA-Z0-9-])", "(-{2,})" );
    $ReplaceWith = array("-", "", "-");
    return preg_replace($RemoveChars, $ReplaceWith, $Raw);
} 

function build_job_link($trade,$area,$id){

$link = '/job/'.clean_file_name($trade).'/'.clean_file_name($area).'/'.$id;

return $link;

}

function build_tradesman_link($company_name,$id){

	// trades
	
	$trades = array();
	$sql = "SELECT url FROM tradesman_to_trade 
			LEFT JOIN trades ON tradesman_to_trade.trade_id = trades.id
			WHERE tradesman_to_trade.tradesman_id = '".$id."' LIMIT 0,4";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	while($rs = mysql_fetch_assoc($query)){
	$trades[] = $rs['url'];
	}
	
	// area
	
$sql = "SELECT url FROM areas 
		LEFT JOIN addresses ON areas.id = addresses.area_id 
		LEFT JOIN tradesman ON addresses.id = tradesman.address_id
		WHERE tradesman.id = '".$id."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$address_rs=mysql_fetch_assoc($query);
	

$link = '/'.implode('-', $trades).'/'.$address_rs['url'].'/'.clean_file_name($company_name).'/tradesman/'.$id;

return $link;

}

?>